using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace StockTradTest
{
    class WxPusher
    {
        //关于Wxpusher更多的使用信息，请关注WXPusher公众号，该功能需自行配置，相应参数需自行修改，切勿直接使用
        public static void HttpPost(string content)
        {

            string _url = "http://wxpusher.zjiecode.com/api/send/message";
            //json参数
            string jsonParam = ToPostJson(content);
            var request = (HttpWebRequest)WebRequest.Create(_url);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            Console.WriteLine(jsonParam);
            byte[] byteData = Encoding.UTF8.GetBytes(jsonParam);
            int length = byteData.Length;
            request.ContentLength = length;
            Stream writer = request.GetRequestStream();
            writer.Write(byteData, 0, length);
            writer.Close();
            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
            Console.WriteLine(responseString.ToString());
        }
        /*
        {
            "appToken":"AT_xxx",
            "content":"Wxpusher祝你中秋节快乐!",
            "summary":"消息摘要",//消息摘要，显示在微信聊天页面或者模版消息卡片上，限制长度100，可以不传，不传默认截取content前面的内容。
            "contentType":1,//内容类型 1表示文字  2表示html(只发送body标签内部的数据即可，不包括body标签) 3表示markdown 
            "topicIds":[ //发送目标的topicId，是一个数组！！！，也就是群发，使用uids单发的时候， 可以不传。
            123
            ],
            "uids":[//发送目标的UID，是一个数组。注意uids和topicIds可以同时填写，也可以只填写一个。
            "UID_xxxx"
            ],
            "url":"http://wxpusher.zjiecode.com" //原文链接，可选参数
        }*/
        static string ToPostJson(string content)
        {
            StringWriter sw = new StringWriter();
            JsonWriter writer = new JsonTextWriter(sw);

            writer.WriteStartObject();
            writer.WritePropertyName("appToken");
            //appToken 需根据申请的数据修改,下面一行我注释掉了，怕了你们了。。。
            //writer.WriteValue("AT_xGPcpmU9cF8txsYSJ1m17QWss49ru0Z0");
            writer.WritePropertyName("content");
            writer.WriteValue(content);
            writer.WritePropertyName("summary");
            //summary 需根据申请的数据修改
            writer.WriteValue("股票交易信息");
            writer.WritePropertyName("contentType");
            writer.WriteValue(1);
            writer.WritePropertyName("topicIds");
            writer.WriteNull();
            writer.WritePropertyName("uids");
            writer.WriteStartArray();
            //uids 需根据申请的数据修改,下面一行我注释掉了，怕了你们了。。。
            //writer.WriteValue("UID_rZnjelrcT084HALHKnJcoGjoLPcO");
            writer.WriteEndArray();
            writer.WriteEndObject();
            writer.Flush();

            return sw.GetStringBuilder().ToString();
        }

        //http://wxpusher.zjiecode.com/api/send/message/?appToken=AT_qHT0cTQfLwYOlBV9cJj9zDSyEmspsmyM&content=123&uid=c1BcpqxEbD8irqlGUh9BhOqR2BvH8yWZ&url=http%3a%2f%2fwxpusher.zjiecode.com

        public static void HttpGet(string content)
        {
            string _url = "http://wxpusher.zjiecode.com/api/send/message";
            string postDataStr = ToGetString(content);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
        }

        static string ToGetString(string content)
        {
            //拼接字符串，根据需要自行修改，切勿直接使用
            string getString = string.Format("appToken=AT_xGPcpmU9cF8txsYSJ1m17QWss49ru0Z0&content={0}&uid=UID_rZnjelrcT084HALHKnJcoGjoLPcO", content);

            return getString;
        }
    }
}
